package com.uum.mq.listener;

import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.uum.mq.config.MqProperties;
import lombok.Setter;
import lombok.SneakyThrows;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@ConfigurationProperties(prefix = "qq.mail")
@Setter
public class MailListener {

    private String host;
    private int port;
    private String user;
    private String password;
    private String title;


    @RabbitListener(queues = "blog-email")
    public void sendMail(Map<String, String> map) {
        if (map != null) {
            sendEmail(map.get("receiver"), map.get("text"));
        }
    }

    /**
     * 发送邮件
     *
     * @param receiver
     * @param text
     */
    @SneakyThrows
    public void sendEmail(String receiver, String text) {
        MailAccount account = new MailAccount();
        account.setHost(host);
        account.setPort(port);
        account.setAuth(true);
        account.setFrom(user);
        account.setUser(user);
        account.setPass(password);
        // 使用SSL安全连接
        account.setSslEnable(true);
        //指定实现javax.net.SocketFactory接口的类的名称,这个类将被用于创建SMTP的套接字
        account.setSocketFactoryClass("javax.net.ssl.SSLSocketFactory");
        account.setSocketFactoryFallback(true);
        MailUtil.send(account,receiver, title, text, true);
    }

    public static void main(String[] args) {
        MailAccount account = new MailAccount();
        account.setHost("smtp.163.com");
        account.setPort(465);
        account.setAuth(true);
        account.setFrom("pphubcc@163.com");
        account.setUser("pphubcc@163.com");
        account.setPass("IGQVKPVJVSKNSZZU");
        // 使用SSL安全连接
        account.setSslEnable(true);
        //指定实现javax.net.SocketFactory接口的类的名称,这个类将被用于创建SMTP的套接字
        account.setSocketFactoryClass("javax.net.ssl.SSLSocketFactory");
        account.setSocketFactoryFallback(true);
        MailUtil.send(account,"630948815@qq.com", "bbbb", "ccccc", true);
    }
}
